" 1.1.for遍历列表
let animals = ['cat','dog','parrot']

for animal in animals
    echo animal
endfor

" 1.2.for遍历字典
let animal_names = {
    \ 'cat' : 'Miss Cattington',
    \ 'dog' : 'Mr Dogson',
    \ 'parrot' : 'Polly'
\ }

for animal in keys(animal_names)
    echo 'This ' . animal . '''s name is ' . animal_names[animal]
endfor

" 1.3.for遍历字典，同时访问键，值
for [animal,name] in items(animal_names)
    echo 'This ' . animal . '''s name is ' . name 
endfor

" 2.break终止循环
for animal in animals
    if animal == 'cat'
        echo 'It''s a cat! Breaking!'
        break
    endif
    echo 'Looking at a ' . animal . ', it''s not a cat yet...'
endfor

" 3.continue 跳过当前循环
for animal in animals
    if animal == 'cat'
        echo 'Ignoring the cat...'
        continue
    endif
    echo 'Looking at a ' . animal
endfor

"4.while循环
while ! empty(animals)
    echo remove(animals, 0)
endwhile

" 5.在while使用break和continue
let animals = ['cat','dog','parrot']

while len(animals) > 0
    let animal =remove(animals,0)
    if animal == 'dog'
        echo 'Encountered a dog, breaking!'
        break
    endif
    echo 'Looking at a ' . animal
endwhile